package com.panghu.controller;


import com.panghu.entity.*;
import com.panghu.service.FillQuestionService;
import com.panghu.service.JudgeQuestionService;
import com.panghu.service.MultiQuestionService;
import com.panghu.service.PaperManageService;
import com.panghu.service.impl.FillQuestionServiceImpl;
import com.panghu.service.impl.JudgeQuestionServiceImpl;
import com.panghu.service.impl.MultiQuestionServiceImpl;
import com.panghu.utils.ApiResultHandler;
import com.sun.org.apache.bcel.internal.generic.RETURN;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 试卷管理表 前端控制器
 * </p>
 *
 * @author huahua
 * @since 2022-03-19
 */
@RestController
public class PaperManageController {

    @Autowired
    private PaperManageService paperManageService;

    @Autowired
    private JudgeQuestionService judgeQuestionService;

    @Autowired
    private MultiQuestionService multiQuestionService;

    @Autowired
    private FillQuestionService fillQuestionService;


    @GetMapping("/papers")
    public ApiResult<PaperManage> findAll() {
        //查找所有试卷的id questionId
        return ApiResultHandler.success(paperManageService.list());
    }

    @GetMapping("/paper/{paperId}")
    public Map<Integer, List<?>> findById(@PathVariable("paperId") Integer paperId) {
        //查询所有类型的题目
        List<JudgeQuestion> judge = judgeQuestionService.findByIdAndType(paperId);
        List<MultiQuestion> multi = multiQuestionService.findByIdAndType(paperId);
        List<FillQuestion> fill = fillQuestionService.findByIdAndType(paperId);
        Map<Integer,List<?>> map = new HashMap();
        //选择题 类型1
        map.put(1,multi);
        //填空 类型2
        map.put(2,fill);
        //填空题 类型3
        map.put(3,judge);
        return map;
    }

    @PostMapping("/paperManage")
    public ApiResult add(@RequestBody PaperManage paperManage) {
        boolean save = paperManageService.save(paperManage);
        if(save){
            return ApiResultHandler.buildApiResult(200,"添加成功",save);
        }
        return ApiResultHandler.buildApiResult(400,"添加失败",save);
    }

}

